package com.lottery.im.module.login.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.login.presenter.RegisterPwdPresenter;
import com.lottery.im.module.login.view.RegisterPwdView;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.widget.dialog.ConfirmWarnDialogBuilder;

/*
 * 注册设置密码
 * */
public class RegisterPwdFragment extends BaseSwipeFragment<RegisterPwdPresenter> implements
    RegisterPwdView {

  private final static String PHONE_KEY = "PHONE_KEY";
  private final static String CODE_KEY = "CODE_KEY";
  private final static String INVITE_CODE_KEY = "INVITE_CODE_KEY";

  public static RegisterPwdFragment create(String phone, String code, String inviteCode) {
    RegisterPwdFragment fragment = new RegisterPwdFragment();
    Bundle b = new Bundle();
    b.putString(PHONE_KEY, phone);
    b.putString(CODE_KEY, code);
    b.putString(INVITE_CODE_KEY, inviteCode);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.et_pwd1) EditText mEtPwd1;
  @BindView(R.id.et_pwd2) EditText mEtPwd2;

  private String mPhone;
  private String mCode;
  private String mInviteCode;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_password_set;
  }

  @Override protected void initPresenter() {
    presenter = new RegisterPwdPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {

    StatusBarHelper.setStatusBarLightMode(getActivity());

    if (getArguments() == null) {
      popBackStack();
      return;
    } else {
      mPhone = getArguments().getString(PHONE_KEY);
      mCode = getArguments().getString(CODE_KEY);
      mInviteCode = getArguments().getString(INVITE_CODE_KEY);
    }
  }

  @OnClick({ R.id.iv_back, R.id.btn_next }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        back();
        break;
      case R.id.btn_next:
        setPwd();
        break;
    }
  }

  private void back() {

    ConfirmWarnDialogBuilder.getInstance(mActivity)
        .setContent(getString(R.string.confirm_give_up_reg))
        .setCancel(getString(R.string.cancel), view -> {

        })
        .setConfirm(getString(R.string.confirm), view -> popBackStack())
        .show();
  }

  /*
   * 设置密码完成 ，下一步
   * */
  private void setPwd() {
    String pwd1 = mEtPwd1.getText().toString().trim();
    String pwd2 = mEtPwd2.getText().toString().trim();

    if (TextUtils.isEmpty(pwd1)) {
      showToast(R.string.hint_enter_password);
      return;
    }
    if (TextUtils.isEmpty(pwd2)) {
      showToast(R.string.hint_enter_password_again);
      return;
    }
    if (pwd1.length() < 6 || pwd1.length() > 20) {
      showToast(R.string.hint_password);
      return;
    }
    if (!pwd1.equals(pwd2)) {
      showToast(R.string.pwd_different);
      return;
    }
    startFragmentAndDestroyCurrent(RegisterNameFragment.create(mPhone, mCode, mInviteCode, pwd1));
  }
}
